package com.tk.itext7demo.utils.excel;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.IOException;

/**
 * @program: easy
 * @description:
 * @author: tjw
 * @create: 2022-11-11 10:11
 **/
public class PaginationEventHandler implements IEventHandler {
    protected PdfFormXObject background;

    public PaginationEventHandler(PdfFormXObject background) throws IOException {
        this.background = background;
    }

    @Override
    public void handleEvent(Event event) {
        PdfDocument pdfDoc = ((PdfDocumentEvent) event).getDocument();
        // Add the background
        new PdfCanvas(((PdfDocumentEvent) event).getPage().newContentStreamBefore(),
                ((PdfDocumentEvent) event).getPage().getResources(), pdfDoc).addXObject(background, 0, 0);
    }
}
